package org.example.taskmaster.fragments;

import org.example.taskmaster.R;
import org.example.taskmaster.Task;
import org.example.taskmaster.activities.MainActivity;
import org.example.taskmaster.database.DatabaseAdapter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class TaskDetailsFragment extends DetailsFragment {

	// references to views
	private TextView mNameText;
	private TextView mDateText;
	private TextView mTimeText;
	private TextView mListText;
	private TextView mNotesText;
	private Button mEditTask;
	private Button mDeleteTask;
	// 
	private Task task;
	private Long mRowId;

	public TaskDetailsFragment(Long rowId) {
		mRowId = rowId;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (container == null) {
			return null;
		}
		View mainView = inflater.inflate(R.layout.taskdetails_fragment_layout, container, false);

        // get database
		dbAdapter = new DatabaseAdapter(getActivity());
		dbAdapter.open();
		
        // get references to Views
		mNameText = (TextView) mainView.findViewById(R.id.taskdetails_name);
		mNotesText = (TextView) mainView.findViewById(R.id.taskdetails_notes);
		mDateText = (TextView) mainView.findViewById(R.id.taskdetails_date);
		mTimeText = (TextView) mainView.findViewById(R.id.taskdetails_time);
		mListText = (TextView) mainView.findViewById(R.id.taskdetails_list);
		mEditTask = (Button) mainView.findViewById(R.id.taskdetails_edit);
		mDeleteTask = (Button) mainView.findViewById(R.id.taskdetails_delete);

		// registers an anonymous listener for the Edit Button
		mEditTask.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
  		  		// refer to MainActivity to load a fragment into details area
        		MainActivity main = (MainActivity) getActivity();
        		main.showEditTask(mRowId);
			}
		});
		// registers an anonymous listener for the Delete Button
		mDeleteTask.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				dbAdapter.deleteTask(mRowId);
  		  		// refer to MainActivity to load a fragment into details area
        		MainActivity main = (MainActivity) getActivity();
        		main.showAllTasks();
			}
		});

		// get reference to a Task
		this.getTask();

		// populate TextViews with data from the Task
		this.populate();
		
		return mainView;
	}
	private void getTask() {
		// get reference to a Task
		this.task = null;
		if (mRowId != null) {
			this.task = dbAdapter.getTask(mRowId);
		}
		
		// handle invalid Task
		if (this.task == null) {
    		Toast.makeText(getActivity().getApplicationContext(), "Invalid Task", Toast.LENGTH_SHORT).show();
    		// refer to MainActivity to load a fragment into details area
    		MainActivity main = (MainActivity) getActivity();
    		main.showAllTasks();
		}

	}

	private void populate() {

		// populate TextViews with data from the Task
		mNameText.setText( this.task.getName());
		mNotesText.setText("Notes:" + this.task.getNotes());
		mDateText.setText("Due:" + this.task.getDate());
		mListText.setText("List:" + this.task.getList());
		mTimeText.setText("Due:" + this.task.getTime());


	}

}
